package com.aiyige.utils.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aiyige.R;

/**
 * 确认对话框
 * @author dance
 *
 */
public class ConfirmDialog extends Dialog {
	private Context context;
	private TextView titleView;
	private TextView msgView;
	private Button cancelButton;
	private Button okButton;

	private String title;
	private String msg;
	private String okStr;
	private String cancelStr;
	private View.OnClickListener okListener;
	private View.OnClickListener cancelListener;

	public ConfirmDialog(Context context) {
		super(context, R.style.confirm_dialog);
		this.context = context;
		setCanceledOnTouchOutside(false);
	}

	public void setTitle(int resId) {
		title = context.getString(resId);
	}

	public void setTitle(String text) {
		title = text;
	}

	public void setMessage(int resId) {
		msg = context.getString(resId);
	}

	public void setMessage(String text) {
		msg = text;
	}

	public void setOkListener(String okStr, View.OnClickListener okListener) {
		this.okStr = okStr;
		this.okListener = okListener;
	}

	public void setCancelListener(String cancelStr, View.OnClickListener cancelListener) {
		this.cancelStr = cancelStr;
		this.cancelListener = cancelListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_layout);
		titleView = (TextView) findViewById(R.id.title);
		msgView = (TextView) findViewById(R.id.msg);
		cancelButton = (Button) findViewById(R.id.cancel);
		okButton = (Button) findViewById(R.id.ok);

		if (TextUtils.isEmpty(title)) {
			titleView.setVisibility(View.GONE);
		}else {
			titleView.setVisibility(View.VISIBLE);
			titleView.setText(title);
		}

		if (!TextUtils.isEmpty(msg)) {
			msgView.setText(msg);
		}

		if (TextUtils.isEmpty(okStr) || okListener == null) {
			findViewById(R.id.btn_line).setVisibility(View.GONE);
			okButton.setVisibility(View.GONE);
		}else {
			okButton.setText(okStr);
			okButton.setOnClickListener(okListener);
		}

		if (TextUtils.isEmpty(cancelStr) || cancelListener == null) {
			findViewById(R.id.btn_line).setVisibility(View.GONE);
			cancelButton.setVisibility(View.GONE);
		}else {
			cancelButton.setText(cancelStr);
			cancelButton.setOnClickListener(cancelListener);
		}
	}

}
